package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Dish;
import com.example.service.DishService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("dishs")
public class DishController {
    
    @Autowired
    private DishService dishService;


    /**
     * 分页查询所有菜品信息
     * @return
     */
    @GetMapping("/getPageDish/{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,Dish dish){

        IPage page=new Page(currentPage,pageSize);
        LambdaQueryWrapper<Dish> lqw =new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(dish.getName()),Dish::getName,dish.getName());

        IPage page1 = dishService.page(page,lqw);


        return R.success("分页查询所有dishs成功！",page1);
    }

    /**
     * 根据分类查询所有菜品信息
     * @return
     */
    @GetMapping("/getByFlavorType")
    public R getByFlavorType(Dish dish){

        LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(dish.getFlavorType()),Dish::getFlavorType,dish.getFlavorType());
        List<Dish> list = dishService.list(lqw);

        return R.success("根据分类查询所有菜品信息！",list);
    }

    /**
     * 查询所有菜品信息
     * @return
     */
    @GetMapping("/getAll")
    public R getAll(){

        List<Dish> list = dishService.list();

        return R.success("查询所有菜品信息！",list);
    }



    /**
     * 添加
     * @param dish
     * @return
     */
    @PostMapping("/addDish")
    public R add(@RequestBody Dish dish){

        LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(dish.getName()),Dish::getName,dish.getName());
        Dish dish1=dishService.getOne(lqw);

        if (dish1==null) {
            Boolean update = dishService.save(dish);
            if (update) {
                return R.success("添加dish成功!", null);
            }
        }
        return R.error("菜品已存在");
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteDish/{id}")
    public R deleteById(@PathVariable int id){

        Boolean byId = dishService.removeById(id);
        if (byId){
            return R.success("删除dishs成功!",null);
        }
        return R.error("删除dishs失败!");
    }

    /**
     * 修改
     * @param dish
     * @return
     */
    @PutMapping("/updateDish")
    public R deleteById(@RequestBody Dish dish){

        Boolean update = dishService.updateById(dish);
        if (update){
            return R.success("修改dish成功!",dish);
        }
        return R.error("修改dish失败!");
    }


}

